package com.banfan.almond.web.service.impl;

import com.banfan.almond.base.global.SQLConf;
import com.banfan.almond.base.serviceImpl.SuperServiceImpl;
import com.banfan.almond.entity.entity.Admin;
import com.banfan.almond.entity.vo.AdminVO;
import com.banfan.almond.utils.RedisUtil;
import com.banfan.almond.utils.ResultData;
import com.banfan.almond.utils.ResultUtil;
import com.banfan.almond.web.mapper.AdminMapper;
import com.banfan.almond.web.security.JwtUtil;
import com.banfan.almond.web.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 管理员表 服务实现类
 *
 * @author 陌溪
 * @since 2018-09-04
 */
@Slf4j
@Service
public class AdminServiceImpl extends SuperServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AdminService adminService;


    @Override
    public Admin getUserByName(String name) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.eq(SQLConf.USER_NAME,name);
        return adminService.getOne(adminQueryWrapper);
    }


    @Override
    public String login(AdminVO adminVO){

//        String username = adminVO.getUsername();
//        String password = adminVO.getPassword();
//
//        //看看有没有用户(查密码)
//        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
//        adminQueryWrapper.eq(SQLConf.USER_NAME,username);
//        adminQueryWrapper.eq(SQLConf.PASS_WORD,password);
//
//        Admin admin = adminService.getOne(adminQueryWrapper);
//        if (admin == null) {
//            return ResultUtil.errorWithMessage("error login");
//        }
        String username = adminVO.getUsername();
        String token = JwtUtil.createJWT(username,30);
        redisUtil.setEx(username,token,10,TimeUnit.MINUTES);


        return ResultUtil.successWithData(token);
    }

    @Override
    public String register(AdminVO adminVO) {

        String username = adminVO.getUsername();

        if (getUserByName(username) != null) {
            return ResultUtil.errorWithMessage(String.format("exist register {}",username));
        }

        String password = adminVO.getPassword();
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        adminService.save(admin);

        String token = JwtUtil.createJWT(username,2);
        return ResultUtil.successWithData(token);

    }

    @Override
    public String logout(AdminVO adminVO) {

        String username = adminVO.getUsername();
        Boolean delete = redisUtil.delete(username);
        if (!delete){
            return ResultUtil.errorWithMessage("fail logout");
        }
        return ResultUtil.successWithMessage("success logout");
    }


}
